#include "main.h"

void client_read_cp(int CC)
{
	int pox=1;
	struct cp_date red={0};
	if(read(CC,&red,sizeof(red))==0)return;
	printf("接收客户端数据:\n");
	printf("\t\t需要拷贝的文件<%s>，大小为:%ld\n",red.filename,red.date.st_size);
	char openfile[10]="001cp/";
	strcat(openfile,red.filename);

	FILE*fp=fopen(openfile,"w");
	if(fp==NULL){perror("创建并打开失败");return;}
	printf("创建并打开文件成功\n");
	pox=0;
	write(CC,&pox,4);
	long testsize=red.date.st_size;
	char buf[1024]={0};
	int dd;
	char tt[51]={0};
	while(testsize>0)
	{
		memset(buf,1024,0);
		//printf("开始拷贝—>读\n");
		int ret=read(CC,buf,1024);
		//printf("开始拷贝—>传\n");
		fwrite(buf,1,ret,fp);
		testsize-=ret;
		//printf("拷贝剩余:%ld\n",testsize);
		dd=(1.0-(float)testsize/red.date.st_size)*100;
		tt[dd/2]='#';
		tt[50]='\0';
		printf("\r拷贝进度:[%-50s",tt);
		printf("]%d%%",dd);
		/*
		for(int i=0;i<dd;i+=2)
			printf("#");
		printf("]");
		*/
		fflush(stdout);
		usleep(20000);
	}
	fclose(fp);
	printf("\n拷贝完成,总大小为%ld\n",red.date.st_size);
}
